package com.lanou.musicproject.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.RotateInfoAdapter;
import com.lanou.musicproject.bean.RotateInfo;
import com.lanou.musicproject.tools.ListViewForScrollView;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

import java.util.List;

/**
 * Created by 王世美 on 15/10/28.
 * 轮播图内容,歌单详细内容
 */
public class RotateInfoActivity extends Activity {
    // 定义实体类数组
    private List<RotateInfo.ContentEntity> data;
    //
    private String info;
    private String title;
    private String id;
    // 定义自定义listview
    private ListViewForScrollView rotateLV;
    private ImageView backdrop;
    private ImageView backIV,shareIV;
    // 定义一个request请求队列
    private RequestQueue requestQueue;
    private RotateInfoAdapter adapter;
    private Toolbar toolbar;
    private CollapsingToolbarLayout collapsing_toolbar;
    private TextView numTV;
    private TextView infoTV;
    private String des;
    private FloatingActionButton rota_itemViewList;
    // 分享
    final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
    //   private ImageLoader imageLoader;
    // 接口网址
    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
            "&method=baidu.ting.diy.gedanInfo&format=json&listid=";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 加载布局
        setContentView(R.layout.fragment_rotate_info);
        // 初始化组件
        rotateLV = (ListViewForScrollView) findViewById(R.id.rotateLV);
        backdrop = (ImageView) findViewById(R.id.backdrop);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        collapsing_toolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        numTV = (TextView) findViewById(R.id.numTV);
        infoTV = (TextView) findViewById(R.id.infoTV);
        shareIV = (ImageView) findViewById(R.id.shareIV);
        backIV = (ImageView) findViewById(R.id.backIV);
        // 对返回按钮设置返回事件
        backIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        requestQueue = Volley.newRequestQueue(this);
        // 接收数据
        Intent intent = getIntent();
        id = intent.getStringExtra("listid");
        this.addData();

        // 通过分享按钮或其他方法弹出分享面板
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
        shareIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 是否只有已登录用户才能打开分享选择页
                mController.openShare(RotateInfoActivity.this, false);
            }
        });

    }

    // 解析实体类数据
    public void addData() {
        final String getUrl = path + id;
        Log.i("wang",getUrl);
        StringRequest request = new StringRequest(getUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    RotateInfo result = gson.fromJson(response, RotateInfo.class);
                    if (result != null && (data = result.getContent()) != null) {
                        adapter = new RotateInfoAdapter(RotateInfoActivity.this, data);
                        rotateLV.setAdapter(adapter);
                        numTV.setText(data.size() + "");
                        String url = result.getUrl();
                        // 设置分享内容
                        mController.setShareContent("施美音乐,让你的世界更美好"+ url);
                        // 设置分享歌单,参数2为歌单的url地址

                    }
                    info = result.getPic_500();
                    title = result.getTitle();
                    des = result.getDesc();
                    Picasso.with(RotateInfoActivity.this).load(info).into(backdrop);
                    toolbar.setTitle(title);
                    infoTV.setText(des);
//                    collapsing_toolbar.setExpandedTitleColor(0xffFFFFFF);
//                    collapsing_toolbar.setCollapsedTitleTextColor(0xffffffff);
                    collapsing_toolbar.setExpandedTitleColor(0xffCCCCFF);
                    collapsing_toolbar.setCollapsedTitleTextColor(0xffCCCCFF);
                    //透明度 只在标题栏上显示
                    collapsing_toolbar.setExpandedTitleColor(0x00000000);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }
}
